<template>
  <div style="width: 100%; height: 100vh; background-color: white; overflow: hidden; position: relative;">
    <div style="width: 400px; margin: 100px auto;">
      <div style="color: black; font-size: 30px; text-align: center; padding: 30px 0; animation: fadeInDown 1s ease-in-out;">欢迎注册</div>
      <el-form ref="form" :model="form" :rules="rules" size="normal">
        <el-form-item prop="username">
          <el-input :prefix-icon="User" v-model="form.username" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon="Hide" v-model="form.password" show-password />
        </el-form-item>
        <el-form-item prop="confirm">
          <el-input :prefix-icon="Hide" v-model="form.confirm" show-password />
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%; font-weight: bold" type="primary" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 添加一些动态的装饰元素 -->
    <div style="position: absolute; bottom: 0; left: 0; width: 100%; height: 50px; background-image: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1)); animation: fadeInUp 1s ease-in-out 2s forwards;"></div>
    <div style="position: absolute; top: 0; right: 0; width: 50px; height: 100%; background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1)); animation: fadeInLeft 1s ease-in-out 2s forwards;"></div>
  </div>
</template>

<script>
import {Search, User, Lock, Hide} from "@element-plus/icons-vue";
import request from "@/utils/request";

export default {
  name: "UserRegister",
  computed: {
    Hide() {
      return Hide;
    },
    User() {
      return User;
    },
    Search() {
      return Search;
    }
  },
  data() {
    return {
      form: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirm: [
          { required: true, message: '请确认密码', trigger: 'blur' },
        ],
      }
    };
  },
  methods: {
    register() {
      if (this.form.password!== this.form.confirm) {
        this.$message({
          type: "error",
          message: "两次输入的密码不一致，请重新输入"
        });
        return;
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post("/user/register", this.form).then(res => {
            if (res.code === '0') {
              this.$message({
                type: "success",
                message: "注册成功"
              });
              this.$router.push("/UserLogin");
            } else {
              this.$message({
                type: "error",
                message: res.msg
              });
            }
          });
        }
      });
    }
  }
};
</script>

<style scoped>
@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInLeft {
  from {
    opacity: 0;
    transform: translateX(20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}
</style>